home *** CD-ROM | disk | FTP | other *** search
- /*==============================================================================
- File: TCLOCK.H
-
- Copyright (C) 1994-1995 Apple Computer, Inc. All rights reserved.
-
- Description:
-
- This file contains the declaration of TClock, a class that implements
- calendar time in a platform-independent fashion.
-
- Class TClock is the central place where time related functionality is
- defined for Dark Passage and its customers.
-
- Change History: (see the bottom of the file).
- ==============================================================================*/
-
- #ifndef TCLOCK_H
- #define TCLOCK_H
-
-
- //==============================================================================
- // Macros
- //==============================================================================
-
- #if !defined(MACCODE)
- #ifdef WINDOWS
- #define MACCODE(x)
- #define WINCODE(x) x
- #else // MACINTOSH
- #define MACCODE(x) x
- #define WINCODE(x)
- #endif
- #endif
-
-
- //==============================================================================
- // Dependencies
- //==============================================================================
-
- #include "time.h"
- #if defined(MACINTOSH)
- #include <OSUtils.h>
- #include <types.h>
- #else
- #include "wtypes.h"
- #endif
-
-
- //==============================================================================
- // Type Definitions
- //==============================================================================
-
- typedef time_t ClockTicks;
- typedef ClockTicks ASDate;
- typedef ClockTicks ASTime;
- typedef ClockTicks ASTimestamp;
- typedef ClockTicks ServerTime;
- typedef ClockTicks ClientTime;
-
-
- //==============================================================================
- // Constants
- //==============================================================================
-
- const ClockTicks kClockTicksPerSec = (ClockTicks)CLK_TCK;
- const ClockTicks kClockTicksPerMin = kClockTicksPerSec * 60;
- const ClockTicks kClockTicksPerHalfHour = kClockTicksPerMin * 30;
- const ClockTicks kClockTicksPerHour = kClockTicksPerMin * 60;
-
- //
- // The following constants are used by function
- // TClock::ClientTimeDeltaIs(CalendarBasis) to establish the appropriate
- // time delta for the client's time library. See explanation
- // below in class.
- //
- enum CalendarBasis
- {
- Origin_1Jan1900, // e.g., Microsoft Class CTime
- Origin_1Jan1901, // e.g., Borland Class TTime
-
- };
-
- //==============================================================================
- // Class TClock
- //
- // Class TClock is the central place where time related functionality is
- // defined.
- //==============================================================================
-
- #ifdef __cplusplus
-
- class TClock
- {
- //
- // Class Features
- //
-
- public:
-
- //
- // class initialization
- //
- WINCODE(static void pascal TClockPrep(void);)
- MACCODE(static pascal void TClockPrep(void);)
-
- // converting to clock ticks
-
- WINCODE(static ClockTicks pascal SecsToClockTicks(unsigned long secs);)
- MACCODE(static pascal ClockTicks SecsToClockTicks(unsigned long secs);)
- WINCODE(static ClockTicks pascal MinsToClockTicks(unsigned long mins);)
- MACCODE(static pascal ClockTicks MinsToClockTicks(unsigned long mins);)
-
- // converting from clock ticks. The second or minute, as
- // appropriate, that the passed clockTicks appears in is
- // returned
-
- WINCODE(static ClientTime pascal ClockTicksToSecs(ClockTicks ticks);)
- MACCODE(static pascal ClientTime ClockTicksToSecs(ClockTicks ticks);)
- WINCODE(static unsigned long pascal ClockTicksToMinutes(ClockTicks ticks);)
- MACCODE(static pascal unsigned long ClockTicksToMinutes(ClockTicks ticks);)
-
- // accessing the current time since boot
-
- WINCODE(static ClockTicks pascal TicksNow(void); )
- MACCODE(static pascal ClockTicks TicksNow(void); )
-
- // Calendar Time in Dark Passage
- //
- // The use of calendar time by Dark Passage must be carefully understood
- // by the Dark Passage customer. In general, Dark Passage works only with
- // calendar time systems that can express time as nnnn seconds from some
- // base date. Unfortunately for all of us, every vendor offers one or more
- // calendar time packages that are based upon different dates. For example,
- // on the PC, both the Microsoft and Borland compiler packages offer the
- // ANSI time.h library and header, both based upon 1-Jan-1970. Microsoft
- // also offers a CTime class in their C++ package, based upon 1-Jan-1900.
- // Borland offers TTime class based upon 1-Jan-1901. On the Macintosh,
- // the ANSI time.h library is available, but based upon 1-Jan-1904.
- // Alternatively, one can use toolbox functions supplied in the rom (i.e.,
- // GetDateTime(), IUTimeString(), IUDateString(), et al), also based upon
- // 1-Jan-1904.
- //
- // To deal with this milieux, Dark Passage uses calendar time as follows.
- // For a given Dark Passage client (whether pc or mac based), there are
- // two kinds of calendar time being manipulated:
- //
- // 1) Client Time
- // Client time is the calendar time system that the customer is using.
- // Typically, the client software is using one of the calendar time
- // manipulation packages available from the compiler and os manufacturers.
- // Since Dark Passage is itself a user of calendar time, it internally
- // manages its client time, based upon platform, as follows:
- //
- // - Mac clients use the Macintosh Toolbox, which uses seconds since
- // 1-Jan-1904.
- // - PC clients are based upon time.h, which uses seconds since 1-Jan-1970.
- //
- // If the client of Dark Passage happens to use the same calendar time
- // system as Dark Passage, or is willing to express dates in these time
- // schemes when interacting with Dark Passage, nothing needs to be done.
- //
- // However, it may be that the Dark Passage customer is already
- // using a time package in their application that uses another calendar
- // time basis. This may be done, but to do so, the TClock package must be
- // initialized before it is used with the following information:
- //
- // - The difference in seconds between their calendar time bases and
- // the one used by Dark Passage internally (either 1-Jan-1904 or
- // 1-Jan-1970, depending upon mac or pc client).
- //
- // - A flag set TRUE if the base time of the client's package is BEFORE
- // the Dark Passage client time, or FALSE if the client time used in
- // the Dark Passage client's time package is AFTER the Dark Passage
- // client time.
- //
- // The function InitClientTimeDelta(long,Boolean) is supplied for this
- // purpose. With it, the number of seconds and flag are explicitly set
- // before the TClock functions are needed. As defaults, the value of
- // seconds delta value is 0 and the Boolean flag is TRUE.
- //
- // In addition, an initialization function is also available that sets
- // the time delta for the commonly used calendar time libraries.
- // See ClientTimeDeltaIs(CalendarBasis) for more details. This function
- // may be used in lieu of the InitClientTimeDelta(long,Boolean) function,
- // if appropriate.
- //
- // Use typedef ClientTime when declaring or casting client time variables
- // and expressions.
- //
- // 2) Server Time (use typedef ServerTime)
- // Server time is based upon the seconds +/- 12:00:00 AM, 1-Jan-2000.
- // With the current implementation, the earliest expressable time is
- // 2:05:52 AM, 11-June-1934. The latest expressable server time
- // is 6:28:15 AM, 6-February-2040.
- //
- // Use typedef ServerTime when declaring or casting client time variables
- // and expressions.
- //
-
- //
- // Client delta initialization (see explanation of calendar time above)
- //
-
- MACCODE(static pascal void InitClientTimeDelta(long delta,Boolean isEarlier);)
- MACCODE(static pascal Boolean ClientTimeDeltaIs(CalendarBasis aTimePkgBaseTm);)
- WINCODE(static void pascal InitClientTimeDelta(long delta,Boolean isEarlier);)
- WINCODE(static Boolean pascal ClientTimeDeltaIs(CalendarBasis aTimePkgBaseTm);)
-
- //
- // ClientTime<->ServerTime Conversion functions
- //
-
- MACCODE(static pascal ServerTime ClientTimeToServerTime(ClientTime theClientTime);)
- MACCODE(static pascal ClientTime ServerTimeToClientTime(ServerTime theServerTime);)
- WINCODE(static ServerTime pascal ClientTimeToServerTime(ClientTime theClientTime);)
- WINCODE(static ClientTime pascal ServerTimeToClientTime(ServerTime theServerTime);)
-
- //
- // Date<->Seconds Conversions
- //
-
- MACCODE(static pascal void DateToSeconds(DateTimeRec *theDT, ClientTime *seconds);)
- MACCODE(static pascal void SecondsToDate(ClientTime seconds,DateTimeRec *theDT);)
- WINCODE(static void pascal DateToSeconds(struct tm *theDT, ClientTime *seconds);)
- WINCODE(static void pascal SecondsToDate(ClientTime seconds, struct tm *theDT);)
-
- //
- // struct tm -> struct DateTimeRec conversion
- //
- // This routine move the corresponding fields from one structure to the
- // other, adjusting them from a zero-base to a one-base as appropriate
- // (e.g., when tm_mon = 0 it is jan; when month=1 in a DateTimeRec, it is
- // jan).
- //
- // Its purpose is to simplify the porting of mac code to the pc that is
- // heavily based upon DateTimeRec. A good example is the
- // TPboyDeliveryTime::TimeToDeliver() method.
- //
- WINCODE(static void pascal StructTMToDateTimeRec(struct tm *ansiTm,DateTimeRec *macTm);)
- MACCODE(static pascal void StructTMToDateTimeRec(struct tm *ansiTm,DateTimeRec *macTm);)
-
- //
- // Date and Time -> String Conversions
- //
- // A pascal string is formatted in buff. At buff + 1, a c string is
- // available. A buffer of at least FORMAT_BUFFER_SZ is expected.
- //
- enum
- {
- FORMAT_BUFFER_SZ = 256,
- };
- MACCODE(static pascal void FormatTime(ClientTime seconds,Boolean wantSecs,StringPtr buff);)
- MACCODE(static pascal void FormatDate(ClientTime seconds,DateForm format,StringPtr buff);)
- WINCODE(static void pascal FormatTime(ClientTime seconds,Boolean wantSecs,StringPtr buff);)
- WINCODE(static void pascal FormatDate(ClientTime seconds,DateForm format,StringPtr buff);)
-
- //
- // Generalized Date and Time formatting
- //
- // Use the passed format string to format the passed client time in the supplied
- // buffer. Formatting characters are the same as those used by the ansi time.h
- // function, strftime(). See your compiler's run-time documentation for
- // details. As above, a pascal string is formatted in buff. At buff + 1, a
- // c string is available. A buffer of at least FORMAT_BUFFER_SZ is expected.
- //
- MACCODE(static pascal void FormatDateTime(ClientTime seconds,char* format,StringPtr buff);)
- WINCODE(static void pascal FormatDateTime(ClientTime seconds,char* format,StringPtr buff);)
-
- //
- // Access current client calendar time, expressed in seconds since the
- // base time (platform and compiler dependent value).
- //
-
- MACCODE(static pascal void CurrentTime(ClientTime *inSeconds);)
- WINCODE(static void pascal CurrentTime(ClientTime *inSeconds);)
-
- //
- // Time Style determination
- //
- // Determine whether calendar time should be formatted in a Euro-style
- // (e.g., 12Feb1904 or variant) or according to the US style. Determination
- // based upon the appropriate system 'gestalt' methods
- //
- MACCODE(static pascal Boolean EuroTimeStyle();)
- WINCODE(static Boolean pascal EuroTimeStyle();)
- MACCODE(static pascal Boolean USEnglishTimeStyle();)
- WINCODE(static Boolean pascal USEnglishTimeStyle();)
-
- //
- // Time comparison functions
- //
-
- // Determine if both times are in same minute of same hour of same day of same
- // month of same year.
- MACCODE(static pascal Boolean InSameMinute(ClientTime t1, ClientTime t2);)
- WINCODE(static Boolean pascal InSameMinute(ClientTime t1, ClientTime t2);)
-
- //
- // Instance Features
- //
-
- // (none)
- };
-
- //==============================================================================
- // TClock inline function implementations
- //==============================================================================
-
- //------------------------------------------------------------------------------
- // SecsToClockTicks(secs)
- //------------------------------------------------------------------------------
-
- WINCODE(inline ClockTicks _export pascal TClock::SecsToClockTicks(unsigned long secs))
- MACCODE(inline pascal ClockTicks TClock::SecsToClockTicks(unsigned long secs))
- {
- return (secs * kClockTicksPerSec);
- }
-
- //------------------------------------------------------------------------------
- // MinsToClockTicks(secs)
- //------------------------------------------------------------------------------
-
- WINCODE(inline ClockTicks _export pascal TClock::MinsToClockTicks(unsigned long mins))
- MACCODE(inline pascal ClockTicks TClock::MinsToClockTicks(unsigned long mins))
- {
- return (mins * kClockTicksPerMin);
- }
-
- //------------------------------------------------------------------------------
- // TicksNow()
- //
- // Description:
- // Answer the current system time expressed as the number of clock ticks
- // since the system was last started.
- //------------------------------------------------------------------------------
-
- WINCODE(inline ClockTicks _export pascal TClock::TicksNow())
- MACCODE(inline pascal ClockTicks TClock::TicksNow())
- {
- WINCODE(return ((ClockTicks)clock());)
- MACCODE(return ((ClockTicks)TickCount());)
- }
- #endif // __cplusplus
-
- //==============================================================================
- //
- // C API versions of the TClock functions.
- //
- // See the documentation in the class above for the semantics of these functions.
- //
- //==============================================================================
- #ifdef __cplusplus
- extern "C"
- {
- #endif
- // initialization
- WINCODE(void _export pascal ASClockPrep(void);)
- MACCODE(pascal void ASClockPrep(void);)
-
- // converting to clock ticks
- WINCODE(ClockTicks _export pascal ASSecsToClockTicks(unsigned long secs);)
- MACCODE(pascal ClockTicks ASSecsToClockTicks(unsigned long secs);)
- WINCODE(ClockTicks _export pascal ASMinsToClockTicks(unsigned long mins);)
- MACCODE(pascal ClockTicks ASMinsToClockTicks(unsigned long mins);)
-
- // converting from clock ticks
- WINCODE(ClientTime _export pascal ASClockTicksToSecs(ClockTicks ticks);)
- MACCODE(pascal ClientTime ASClockTicksToSecs(ClockTicks ticks);)
- WINCODE(unsigned _export long pascal ASClockTicksToMinutes(ClockTicks ticks);)
- MACCODE(pascal unsigned long ASClockTicksToMinutes(ClockTicks ticks);)
-
- // accessing the current time since boot
- WINCODE(ClockTicks _export pascal ASTicksNow(void); )
- MACCODE(pascal ClockTicks ASTicksNow(void); )
-
- // Client delta initialization (see explanation of calendar time above)
- MACCODE(pascal void ASInitClientTimeDelta(long delta,Boolean isEarlier);)
- MACCODE(pascal Boolean ASClientTimeDeltaIs(CalendarBasis aTimePkgBaseTm);)
- WINCODE(void _export pascal ASInitClientTimeDelta(long delta,Boolean isEarlier);)
- WINCODE(Boolean _export pascal ASClientTimeDeltaIs(CalendarBasis aTimePkgBaseTm);)
-
- // ClientTime<->ServerTime Conversion functions
- MACCODE(pascal ServerTime ASClientTimeToServerTime(ClientTime theClientTime);)
- MACCODE(pascal ClientTime ASServerTimeToClientTime(ServerTime theServerTime);)
- WINCODE(ServerTime _export pascal ASClientTimeToServerTime(ClientTime theClientTime);)
- WINCODE(ClientTime _export pascal ASServerTimeToClientTime(ServerTime theServerTime);)
-
- // Date<->Seconds Conversions
- MACCODE(pascal void ASDateToSeconds(DateTimeRec *theDT, ClientTime *seconds);)
- MACCODE(pascal void ASSecondsToDate(ClientTime seconds,DateTimeRec *theDT);)
- WINCODE(void _export pascal ASDateToSeconds(struct tm *theDT, ClientTime *seconds);)
- WINCODE(void _export pascal ASSecondsToDate(ClientTime seconds, struct tm *theDT);)
-
- // struct tm -> struct DateTimeRec conversion
- WINCODE(void _export pascal ASStructTMToDateTimeRec(struct tm *ansiTm,DateTimeRec *macTm);)
- MACCODE(pascal void ASStructTMToDateTimeRec(struct tm *ansiTm,DateTimeRec *macTm);)
-
- // Date and Time -> String Conversions
- MACCODE(pascal void ASFormatTime(ClientTime seconds,Boolean wantSecs,StringPtr buff);)
- MACCODE(pascal void ASFormatDate(ClientTime seconds,DateForm format,StringPtr buff);)
- WINCODE(void _export pascal ASFormatTime(ClientTime seconds,Boolean wantSecs,StringPtr buff);)
- WINCODE(void _export pascal ASFormatDate(ClientTime seconds,DateForm format,StringPtr buff);)
-
- // Generalized Date and Time formatting
- MACCODE(pascal void ASFormatDateTime(ClientTime seconds,char* format,StringPtr buff);)
- WINCODE(void _export pascal ASFormatDateTime(ClientTime seconds,char* format,StringPtr buff);)
-
- // Access current client calendar time
- MACCODE(pascal void ASCurrentTime(ClientTime *inSeconds);)
- WINCODE(void _export pascal ASCurrentTime(ClientTime *inSeconds);)
-
- // Time Style determination
- MACCODE(pascal Boolean ASEuroTimeStyle();)
- WINCODE(Boolean _export pascal ASEuroTimeStyle();)
- MACCODE(pascal Boolean ASUSEnglishTimeStyle();)
- WINCODE(Boolean _export pascal ASUSEnglishTimeStyle();)
-
- // Calendar time comparisons
- MACCODE(pascal Boolean ASInSameMinute(ClientTime t1, ClientTime t2);)
- WINCODE(Boolean pascal ASInSameMinute(ClientTime t1, ClientTime t2);)
-
- #ifdef __cplusplus
- }
- #endif
-
- #endif // TCLOCK_H
-
-
- // Change History: (prior to cut over to source safe based log)
- //
- // Date By Description
- // ---- -- -----------
- // 03/23/94 SW Added kClockTicksPerHalfHour and kClockTicksPerHour.
- //
- // $Log: /bogart/dp/shared/TCLOCK.H $
- //
- // 9 12/21/94 7:37p Rich_johnson
- //
-
-